//地音 算法特征值
package com.safety.algorithm.eigenvalue.geosound;


import com.safety.algorithm.model.geosound.*;

import java.util.*;

public class GeosoundAlgorithm {

    private final int geosoundPoklad = 100000; // 大能量地音事件的阈值

    /**
     * @Description: 平均值计算
     * @Param: [lists List<Map<String, Object>>]
     * @return: double 平均值
     * @Author: ChengQi
     * @Date: 2018/12/21
     */
    public static double average(List<Map<String, Object>> lists) {
        double sum = 0;
        double count = lists.size();
        for (Map<String, Object> map : lists) {
            if (map.get("emergoa").equals(-1)) {
                count--;
                continue;
            } else {
                sum += Double.parseDouble(map.get("emergoa").toString());
            }
        }
        return count == 0 ? 0 : sum / count;
    }

    public static double averageDouble(List<Double> lists) {
        double sum = 0;
        double count = lists.size();
        for (Double emergoa : lists) {
            if (emergoa == -1) {
                count--;
            } else {
                sum += emergoa;
            }
        }
        return count == 0 ? 0 : sum / count;
    }

    // 偏差高值总数
    //type == 1 能量
    //type == 2 脉冲
    public static double deviationTotal(List<Map<String, Object>> lists, int type) {
        int sum = 0;
        String element = null;
        if (type == 1) {
            element = "emergoaFlag";
        } else {
            element = "pulseFlag";
        }
        for (Map<String, Object> map : lists) {
            if (map.get(element).toString().equals("1")) {
                sum++;
            }
        }
        return sum;
    }

    // 判断是否为大能量地音事件 传入一条地音数据，设置flag后返回
    public GeosoundAlgModel computingGeosoundFlag(GeosoundAlgModel geosoundData) {
        geosoundData.setGeosoundFlag(false);
        if (geosoundData.getEmergoa() > this.geosoundPoklad) {
            geosoundData.setGeosoundFlag(true);
        }
        return geosoundData;
    }

    // 2.2.1判断是否为大能量地音事件 传入一堆地音数据，逐条设置flag后一起返回
    public List<GeosoundAlgModel> computingGeosoundFlag(List<GeosoundAlgModel> listDatas) {
        List<GeosoundAlgModel> result = new ArrayList<GeosoundAlgModel>();
        for (GeosoundAlgModel listData : listDatas) {
            listData.setGeosoundFlag(false);
            if (listData.getEmergoa() > this.geosoundPoklad) {
                listData.setGeosoundFlag(true);
            }
            result.add(listData);
        }
        return result;
    }

    // 2.2.2前置计算 ,传入十分钟默认600条数据
    public double GeosoundTenMinuteAvg(List<GeosoundAlgModel> listAvgs) {
        double sum = 0;
        double len;
        double emergoa = 0;
        len = listAvgs.size();// 默认600
        Date datenow = null;
        String entId = null;
        int i = 0;
        for (GeosoundAlgModel listAvg : listAvgs) {
            sum += listAvg.getEmergoa();
            i++;
            if (i == len) {
                emergoa = sum / len;
            }
        }
        return emergoa;
    }


    // 2.2.3计算能量偏差值,传入86400条数据，返回144组数据
    public List<GeosoundDevEnergyModel> deviationGeosoundEnergy(List<GeosoundAlgModel> lists) {
        List<GeosoundDevEnergyModel> result = new ArrayList<GeosoundDevEnergyModel>();

        // computingGeosoundAvg
        int count = lists.size();// 默认值144*600
        double summ = 0;
        double sumz = 0;
        double bt = 0;// 24小时平均值
        double pt;// 每十分钟一组值
        double dt;
        for (GeosoundAlgModel list : lists) {
            summ += list.getEmergoa();
        }
        bt = summ / 86400;
        for (GeosoundAlgModel list : lists) {
            for (int i = 0; i < 144; i++) {
                for (int j = 0; j < 600; j++) {
                    sumz += list.getEmergoa();
                }
                pt = sumz / 600;
                sumz = 0;
                dt = pt / bt - 1;
                GeosoundDevEnergyModel geosoundDevEnergyModel =
                    new GeosoundDevEnergyModel(list.getDatetime(), dt, list.getEntId());
                result.add(geosoundDevEnergyModel);// 共返回144组值
            }
        }
        return result;
    }

    // 2.2.4每日能量偏差高值总数,传入一条模型类的值以获取datetime和entid，传入GeosoundDevEnergyModel中算出来的当日所有能量偏差值
    public List<GeosoundDeviationEnergyTotalModel> deviationEnergyTotal(List<GeosoundAlgModel> lists,
        List<GeosoundDevEnergyModel> devEnList) {
        List<GeosoundDeviationEnergyTotalModel> result = new ArrayList<GeosoundDeviationEnergyTotalModel>();

        final int energyPoklad = 2;// 地音事件能量偏差阈值
        double dte;// 能量偏差值
        double dse = 0;// 每日能量偏差高值总数
        for (GeosoundDevEnergyModel geosoundDevEnergyModel : devEnList) {
            if (geosoundDevEnergyModel.getDevEmergoa() > energyPoklad) {
                dse++;
            }
        }
        GeosoundDeviationEnergyTotalModel geosoundDeviationEnergyTotalModel =
            new GeosoundDeviationEnergyTotalModel(lists.get(0).getDatetime(), lists.get(0).getEntId(), dse);
        result.add(geosoundDeviationEnergyTotalModel);
        return result;
    }

    // 2.2.5脉冲，传入前十分钟的所有地音事件，返回统计个数
    public List<GeosoundComputingPulseModel> computingPulse(List<GeosoundAlgModel> lists) {
        List<GeosoundComputingPulseModel> result = new ArrayList<GeosoundComputingPulseModel>();

        final int minPoklad = 10;// 默认时间间隔,暂时没用到
        double pulse = 0;
        for (GeosoundAlgModel GeosoundAlgModel1 : lists) {
            // if (GeosoundAlgModel.isGeosoundFlag()) {
            pulse++;
            // }
        }
        GeosoundComputingPulseModel geosoundComputingPulseModel =
            new GeosoundComputingPulseModel(new Date(), lists.get(0).getEntId(), pulse);
        result.add(geosoundComputingPulseModel);
        return null;
    }

    // 脉冲因子2.2.6 传入36组数据，来自2.2.5脉冲的计算结果
    public List<GeosoundPulseFactorModel> computingPulseFactor(List<GeosoundAlgModel> lists,
        List<GeosoundComputingPulseModel> geoPulseFactor) {
        List<GeosoundPulseFactorModel> result = new ArrayList<GeosoundPulseFactorModel>();
        double pulse[];
        double p;
        pulse = new double[36];
        int i = 0;
        for (GeosoundComputingPulseModel geosoundComputingPulseModel : geoPulseFactor) {
            pulse[i] = geosoundComputingPulseModel.getpulse();
        }
        Arrays.sort(pulse);
        double sum4 = pulse[35] + pulse[34] + pulse[33] + pulse[32];
        sum4 *= 10;
        double sum36 = 0;
        for (i = 35; i > 0; i--) {
            sum36 += pulse[i];
        }
        p = sum4 / sum36;
        GeosoundPulseFactorModel geosoundPulseFactorModel =
            new GeosoundPulseFactorModel(new Date(), lists.get(0).getEntId(), p);
        result.add(geosoundPulseFactorModel);
        return result;
    }

    // 脉冲偏差值2.2.7 传入86400条数据，返回144组数据
    public List<GeosoundDevPulseModel> deviationGeosoundPulse(List<GeosoundAlgModel> lists,
        List<GeosoundComputingPulseModel> pulselist) {
        List<GeosoundDevPulseModel> result = new ArrayList<GeosoundDevPulseModel>();
        int count = lists.size();// 默认值144*600
        double sumbt = 0;
        double sumpt = 0;
        double bt = 0;// 24小时平均值
        double[] pt;// 每十分钟一组值
        pt = new double[145];
        double dt;
        int i = 0;
        for (GeosoundComputingPulseModel geosoundComputingPulseModel : pulselist) {
            sumbt += geosoundComputingPulseModel.getpulse();
            sumpt += geosoundComputingPulseModel.getpulse();
            i++;
            if (i % 600 == 0) {
                pt[i / 600] = sumpt / 600;
            }
        }
        bt = sumbt / 86400;
        for (i = 1; i < 145; i++) {
            dt = pt[i] / bt - 1;
            GeosoundDevPulseModel geosoundDevPulseModel =
                new GeosoundDevPulseModel(new Date(), dt, lists.get(0).getEntId());
            result.add(geosoundDevPulseModel);
        }
        bt = sumbt / 86400;
        return result;
    }

    // 每日脉冲偏差高值总数2.2.8,传入一条GeosoundAlgModel对应日期的数据，传入当日所有脉冲偏差值数据。
    public List<GeosoundDeviationPulseTotalModel> deviationPulseTotal(List<GeosoundAlgModel> lists,
        List<GeosoundDevPulseModel> devEnList) {
        List<GeosoundDeviationPulseTotalModel> result = new ArrayList<GeosoundDeviationPulseTotalModel>();
        final int energyPoklad = 2;// 地音事件脉冲偏差阈值
        double dtp;// 脉冲偏差值
        double dsp = 0;// 每日脉冲偏差高值总数
        for (GeosoundDevPulseModel geosoundDevEnergyModel : devEnList) {
            if (geosoundDevEnergyModel.getDevPulse() > energyPoklad) {
                dsp++;
            }
        }
        GeosoundDeviationPulseTotalModel geosoundDeviationPulseTotalModel =
            new GeosoundDeviationPulseTotalModel(lists.get(0).getDatetime(), lists.get(0).getEntId(), dsp);
        result.add(geosoundDeviationPulseTotalModel);
        return result;
    }

}
